package com.ysq.nfd.http.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpBrushVotes {
	
	String charset = "UTF-8";
	
	private RequestConfig requestConfig = RequestConfig.custom()
			.setSocketTimeout(15000)
			.setConnectTimeout(15000)
			.setConnectionRequestTimeout(15000)
			.build();

	public HttpBrushVotes() {}
	public HttpBrushVotes(String charset) {
		this.charset = charset;
	}
	
	/**
	 * 设置post header
	 * @param httpPost
	 * @param headerMap
	 */
	public void setHeader(HttpPost httpPost, Map<String, String> headerMap){
		for(String key : headerMap.keySet()){
			String value = headerMap.get(key);
			httpPost.setHeader(key, value);
		}
	}
	
	/**
	 * 设置 cookie
	 * @param cookieStore
	 * @param cookieMap
	 */
	public void setCookies(CookieStore cookieStore, Map<String, String> cookieMap){
		for(String key: cookieMap.keySet()){
			String value = cookieMap.get(key);
			BasicClientCookie cookie = new BasicClientCookie(key, value);
			cookie.setVersion(0);
			cookie.setDomain("mp.weixin.qq.com");
			cookie.setPath("/");
			cookieStore.addCookie(cookie);
		}
	}
	
	/**
	 * 设置传参
	 * @param httpPost
	 * @param parameterMap
	 */
	public void setParameters(HttpPost httpPost, Map<String, String> parameterMap){
		List<NameValuePair> nameValueList = new ArrayList<NameValuePair>();
		for (String key : parameterMap.keySet()) {
			String value = parameterMap.get(key);
			BasicNameValuePair nameValue = new BasicNameValuePair(key, value);
			
			nameValueList.add(nameValue);
		}
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValueList, charset));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void sendTicket(){
		
		String actUrl = "http://mp.weixin.qq.com/mp/newappmsgvote";
		
		int intWxtoken = 1866634731;
		int intQQNumber = 150691871;
		int intWxticket = 685877734;
		
		for(int i = 0; i<2000; i++){
			intWxtoken++;
			String wxtoken = intWxtoken + "";
			
			intQQNumber++;
			String qqNumberStr = intQQNumber + "";
			
			intWxticket++;
			String wxticket = intWxticket + "";
			
			String referUrl = "http://mp.weixin.qq.com/mp/newappmsgvote?action=show&__biz=MzA3MDI1NTAyMQ==&supervoteid=619357004&uin=777&key=777"
					+ "&pass_ticket=x5bwYY8ajutTBUp%252FcwGiJg2sYf%252F51bAHUcS%252Fswn3K3gFgQZAaFBXS1DmX9YyAVF6"
					+ "&wxtoken=" + wxtoken + "&mid=2651617809&idx=1";
			
			
			String userAgent = "Mozilla/5.0 (Linux; Android 7.0; MI MAX Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 "
					+ "Mobile MQQBrowser/6.2 TBS/043305 Safari/537.36 MicroMessenger/6.5.8.1060 NetType/WIFI Language/zh_CN";
			
			/** header **/
			Map<String, String> headerMap = new HashMap<String, String>();
			headerMap.put("Accept", "application/json");
			headerMap.put("Origin", "http://mp.weixin.qq.com");
			headerMap.put("userAgent", userAgent);
			headerMap.put("referUrl", referUrl);
			headerMap.put("Accept-Encoding", "gzip, deflate");
			headerMap.put("Q-UA2", "QV=3&PL=ADR&PR=WX&PP=com.tencent.mm&PPVN=6.5.8&TBSVC=43202&CO=BK&COVC=043305&PB=GE&VE=GA&DE=PHONE&CHID=0&LCID=9422&MO= MIMAX &RL=1080*1920&OS=7.0&API=24");
			headerMap.put("Q-GUID", "4a802534a6987da3feebcf8413b788cb");
			headerMap.put("Q-Auth", "31045b957cf33acf31e40be2f3e71c5217597676a9729f1b");
			
			//headerMap.put("aaaaaaaaaaa", "aaaaaaaaaa");
			
			/** cookies **/
			Map<String, String> cookieMap = new HashMap<String, String>();
			cookieMap.put("pgv_pvi", "3707998208");
			cookieMap.put("pgv_si", "s3256897536");
			cookieMap.put("ptisp", "cnc");
			cookieMap.put("RK", "Nf3yXOpfWa");
			cookieMap.put("ptcz", "da0d79dfe0539e28ce60cb9eb8e9984f790ed26b0bbc0a50e14b9b100f58a1fb");
			cookieMap.put("pt2gguin", "o0" + qqNumberStr);
			cookieMap.put("sd_userid", "23291492058525820");
			cookieMap.put("sd_cookie_crttime", "1492058525820");
			cookieMap.put("rewardsn", "ab58cc84b1e8564be7f3");
			cookieMap.put("pgv_info", "ssid=s3462705932");
			cookieMap.put("pgv_pvid", "9668385148");
			cookieMap.put("o_cookie", qqNumberStr);
			cookieMap.put("wap_sid", "CLi6ndoJEkBKdmFwV2lhbWFXQ2FPcUprNlBYSFo2c3h2eVE4czVpdGtkYUE0UWc3QkVDMlNZajB0azNQNVhyWGw0RFA5X0c1GAQg/BEoiJrVnQww8vqPyQU=");
			cookieMap.put("wxtokenkey", "c3bcbe3cfa685c1319fb832833aad6877024090e642bb5e43244949aeff24448");
			cookieMap.put("wxticket", wxticket);
			cookieMap.put("wxticketkey", "5d2e5fc4c3efca555b320f022eb5c4137024090e642bb5e43244949aeff24448");
			cookieMap.put("wxuin", "2605145400");
			cookieMap.put("pass_ticket", "x5bwYY8ajutTBUp/cwGiJg2sYf/51bAHUcS/swn3K3gFgQZAaFBXS1DmX9YyAVF6");
			cookieMap.put("wap_sid2", "CLi6ndoJElw5WUxhVzc4ZXotdURudk11cDVlZlZnVndpNmgtQlEwRk05OVBLSGtIWTNYajR0S0VZYUd6aVhlVHFQV3lkSkhsVm1hU2ZBZlBKNTBWY2VwTi0tdXU0WTBEQUFBfjCGkJ7KBTgNQAE=");
			
			/** parameter **/
			String __biz = "MzA3MDI1NTAyMQ%3D%3D";
			String pass_ticket = "x5bwYY8ajutTBUp%252FcwGiJg2sYf%252F51bAHUcS%252Fswn3K3gFgQZAaFBXS1DmX9YyAVF6";
			String jsonData = "%7B%22super_vote_item%22%3A%5B%7B%22vote_id%22%3A619357005%2C%22item_idx_list%22%3A%7B%22item_idx%22%3A%5B%2216%22%5D%7D%7D%5D%2C%22super_vote_id%22%3A619357004%7D";
			try {
				pass_ticket = URLDecoder.decode(pass_ticket, charset);
				jsonData = URLDecoder.decode(jsonData, charset);
				__biz = URLDecoder.decode(__biz, charset);
				
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("action", "vote");
			paramMap.put("__biz", __biz);
			paramMap.put("uin", "777");
			paramMap.put("key", "777");
			paramMap.put("pass_ticket", pass_ticket);
			paramMap.put("f", "json");
			paramMap.put("json", jsonData);
			paramMap.put("idx", "1");
			paramMap.put("mid", "2651617809");
			paramMap.put("wxtoken", wxtoken);
			
			
			/** ========== 开始 httpclient ========== **/
			
			CookieStore cookieStore = new BasicCookieStore();
			
			CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
			
			HttpPost httpPost = new HttpPost(actUrl);
			
			/** header **/
			setHeader(httpPost, headerMap);
			
			/** cookie **/
			setCookies(cookieStore, cookieMap);
			
			/** parameter **/
			setParameters(httpPost, paramMap);
			
			httpPost.setConfig(requestConfig);
			
			try {
				CloseableHttpResponse response = httpClient.execute(httpPost);
				
				int statusCode = response.getStatusLine().getStatusCode();
				String responseText = response.getStatusLine().toString();
				
				HttpEntity entity = response.getEntity();
				String responseContent = EntityUtils.toString(entity, charset);
				
				
				System.out.println("[" + statusCode + "]" + responseText + "\r\n" + responseContent);
				System.out.println("==================================\r\n\r\n");
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	public static void main(String[] args) {
		System.out.println("start ... ");
		
		HttpBrushVotes hbv = new HttpBrushVotes();
		hbv.sendTicket();
		
		System.out.println("finished ... ");
	}

}
